<template>
  <el-dialog
    :model-value="modelValue"
    @update:model-value="(value) => emit('update:modelValue', value)"
    title="法规分析"
    width="1000px"
    :before-close="handleClose"
  >
    <div v-if="regulation" class="regulation-analysis">
      <!-- 分析概览 -->
      <div class="analysis-overview">
        <h3 class="section-title">分析概览</h3>
        <div class="overview-cards">
          <div class="overview-card">
            <div class="card-icon compliance">
              <el-icon><Check /></el-icon>
            </div>
            <div class="card-content">
              <div class="card-number">{{ analysisData.complianceScore }}%</div>
              <div class="card-label">合规度</div>
            </div>
          </div>
          <div class="overview-card">
            <div class="card-icon risk">
              <el-icon><Warning /></el-icon>
            </div>
            <div class="card-content">
              <div class="card-number">{{ analysisData.riskLevel }}</div>
              <div class="card-label">风险等级</div>
            </div>
          </div>
          <div class="overview-card">
            <div class="card-icon impact">
              <el-icon><TrendCharts /></el-icon>
            </div>
            <div class="card-content">
              <div class="card-number">{{ analysisData.impactScore }}</div>
              <div class="card-label">影响评分</div>
            </div>
          </div>
        </div>
      </div>

      <!-- 法规信息 -->
      <div class="regulation-info">
        <h3 class="section-title">法规信息</h3>
        <div class="info-grid">
          <div class="info-item">
            <label>法规名称：</label>
            <span>{{ regulation.name }}</span>
          </div>
          <div class="info-item">
            <label>法规编号：</label>
            <span>{{ regulation.number }}</span>
          </div>
          <div class="info-item">
            <label>发布机构：</label>
            <span>{{ regulation.publisher }}</span>
          </div>
          <div class="info-item">
            <label>生效日期：</label>
            <span>{{ regulation.effectiveDate }}</span>
          </div>
        </div>
      </div>

      <!-- 分析结果 -->
      <div class="analysis-results">
        <h3 class="section-title">分析结果</h3>
        <el-tabs v-model="activeTab" type="card">
          <el-tab-pane label="合规性分析" name="compliance">
            <div class="analysis-content">
              <div class="analysis-item">
                <h4>合规要点</h4>
                <ul class="compliance-points">
                  <li v-for="point in analysisData.compliancePoints" :key="point.id">
                    <el-icon :class="point.status === 'pass' ? 'text-green-500' : 'text-red-500'">
                      <component :is="point.status === 'pass' ? 'Check' : 'Close'" />
                    </el-icon>
                    <span>{{ point.description }}</span>
                  </li>
                </ul>
              </div>
            </div>
          </el-tab-pane>
          
          <el-tab-pane label="风险评估" name="risk">
            <div class="analysis-content">
              <div class="risk-matrix">
                <h4>风险矩阵</h4>
                <div class="risk-items">
                  <div v-for="risk in analysisData.risks" :key="risk.id" class="risk-item">
                    <div class="risk-header">
                      <span class="risk-title">{{ risk.title }}</span>
                      <el-tag :type="getRiskTagType(risk.level)" size="small">
                        {{ risk.level }}
                      </el-tag>
                    </div>
                    <div class="risk-description">{{ risk.description }}</div>
                    <div class="risk-mitigation">
                      <strong>缓解措施：</strong>{{ risk.mitigation }}
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </el-tab-pane>
          
          <el-tab-pane label="影响分析" name="impact">
            <div class="analysis-content">
              <div class="impact-analysis">
                <h4>影响评估</h4>
                <div class="impact-categories">
                  <div v-for="category in analysisData.impacts" :key="category.name" class="impact-category">
                    <div class="category-header">
                      <span class="category-name">{{ category.name }}</span>
                      <div class="category-score">
                        <el-rate
                          v-model="category.score"
                          disabled
                          show-score
                          text-color="#ff9900"
                          score-template="{value}"
                        />
                      </div>
                    </div>
                    <div class="category-description">{{ category.description }}</div>
                  </div>
                </div>
              </div>
            </div>
          </el-tab-pane>
          
          <el-tab-pane label="建议措施" name="recommendations">
            <div class="analysis-content">
              <div class="recommendations">
                <h4>改进建议</h4>
                <div class="recommendation-list">
                  <div v-for="rec in analysisData.recommendations" :key="rec.id" class="recommendation-item">
                    <div class="rec-priority">
                      <el-tag :type="getPriorityTagType(rec.priority)" size="small">
                        {{ rec.priority }}
                      </el-tag>
                    </div>
                    <div class="rec-content">
                      <div class="rec-title">{{ rec.title }}</div>
                      <div class="rec-description">{{ rec.description }}</div>
                      <div class="rec-timeline">预计完成时间：{{ rec.timeline }}</div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
        <el-button type="primary" @click="exportAnalysis">导出分析报告</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { Check, Warning, TrendCharts, Close } from '@element-plus/icons-vue'

interface Props {
  modelValue: boolean
  regulation?: any
}

interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'success'): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

const activeTab = ref('compliance')

// 模拟分析数据
const analysisData = ref({
  complianceScore: 85,
  riskLevel: '中等',
  impactScore: 7.5,
  compliancePoints: [
    { id: 1, description: '法规条文完整性检查', status: 'pass' },
    { id: 2, description: '执行标准符合性', status: 'pass' },
    { id: 3, description: '时效性验证', status: 'fail' },
    { id: 4, description: '适用范围确认', status: 'pass' }
  ],
  risks: [
    {
      id: 1,
      title: '执行标准不明确',
      level: '中等',
      description: '部分条款的执行标准存在模糊性，可能导致执行偏差',
      mitigation: '制定详细的执行指南和操作规程'
    },
    {
      id: 2,
      title: '监督机制缺失',
      level: '高',
      description: '缺乏有效的监督和检查机制',
      mitigation: '建立定期检查和评估制度'
    }
  ],
  impacts: [
    { name: '运营影响', score: 4, description: '对日常运营的影响程度' },
    { name: '合规风险', score: 3, description: '不合规可能带来的风险' },
    { name: '成本影响', score: 2, description: '执行成本和资源投入' },
    { name: '效率提升', score: 4, description: '对工作效率的提升作用' }
  ],
  recommendations: [
    {
      id: 1,
      priority: '高',
      title: '完善监督机制',
      description: '建立定期检查和评估制度，确保法规有效执行',
      timeline: '3个月'
    },
    {
      id: 2,
      priority: '中',
      title: '制定执行指南',
      description: '针对模糊条款制定详细的执行指南',
      timeline: '2个月'
    },
    {
      id: 3,
      priority: '低',
      title: '培训宣贯',
      description: '组织相关人员进行法规培训',
      timeline: '1个月'
    }
  ]
})

const handleClose = () => {
  emit('update:modelValue', false)
}

const exportAnalysis = () => {
  ElMessage.success('分析报告导出成功')
}

const getRiskTagType = (level: string) => {
  const types: Record<string, string> = {
    '低': 'success',
    '中等': 'warning',
    '高': 'danger'
  }
  return types[level] || 'info'
}

const getPriorityTagType = (priority: string) => {
  const types: Record<string, string> = {
    '高': 'danger',
    '中': 'warning',
    '低': 'info'
  }
  return types[priority] || 'info'
}
</script>

<style scoped>
.regulation-analysis {
  padding: 20px 0;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 16px;
  border-left: 4px solid #409eff;
  padding-left: 12px;
}

.analysis-overview {
  margin-bottom: 24px;
}

.overview-cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

.overview-card {
  display: flex;
  align-items: center;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e9ecef;
}

.card-icon {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
  font-size: 24px;
  color: white;
}

.card-icon.compliance {
  background: #67c23a;
}

.card-icon.risk {
  background: #e6a23c;
}

.card-icon.impact {
  background: #409eff;
}

.card-content {
  flex: 1;
}

.card-number {
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  line-height: 1;
}

.card-label {
  font-size: 14px;
  color: #606266;
  margin-top: 4px;
}

.regulation-info {
  margin-bottom: 24px;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
}

.info-item {
  display: flex;
  align-items: center;
}

.info-item label {
  font-weight: 500;
  color: #606266;
  margin-right: 8px;
  min-width: 80px;
}

.analysis-results {
  margin-bottom: 24px;
}

.analysis-content {
  padding: 16px 0;
}

.compliance-points {
  list-style: none;
  padding: 0;
  margin: 0;
}

.compliance-points li {
  display: flex;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid #f0f0f0;
}

.compliance-points li:last-child {
  border-bottom: none;
}

.compliance-points li .el-icon {
  margin-right: 8px;
  font-size: 16px;
}

.risk-items {
  space-y: 16px;
}

.risk-item {
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e9ecef;
  margin-bottom: 16px;
}

.risk-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.risk-title {
  font-weight: 500;
  color: #303133;
}

.risk-description {
  color: #606266;
  margin-bottom: 8px;
  line-height: 1.5;
}

.risk-mitigation {
  color: #909399;
  font-size: 14px;
  line-height: 1.5;
}

.impact-categories {
  space-y: 16px;
}

.impact-category {
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e9ecef;
  margin-bottom: 16px;
}

.category-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.category-name {
  font-weight: 500;
  color: #303133;
}

.category-description {
  color: #606266;
  font-size: 14px;
  line-height: 1.5;
}

.recommendation-list {
  space-y: 16px;
}

.recommendation-item {
  display: flex;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e9ecef;
  margin-bottom: 16px;
}

.rec-priority {
  margin-right: 16px;
  flex-shrink: 0;
}

.rec-content {
  flex: 1;
}

.rec-title {
  font-weight: 500;
  color: #303133;
  margin-bottom: 8px;
}

.rec-description {
  color: #606266;
  line-height: 1.5;
  margin-bottom: 8px;
}

.rec-timeline {
  color: #909399;
  font-size: 14px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}
</style>